<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use Illuminate\Routing\Controller;

class BaseController extends Controller
{
    protected $title;
    protected $module = [];
    protected $model = false;
    protected $desc = [];
    /**
     * {@inheritdoc}
     */
    protected function title()
    {
        return $this->title ?? 'Title';
    }
    /**
     * Set description for following 4 action pages.
     *
     * @var array
     */

    /**
     * Index interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->desc['index'] ?? trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->desc['show'] ?? trans('admin.show'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed   $id
     * @param Content $content
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->desc['edit'] ?? trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->desc['create'] ?? trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Update the specified resource in storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function update($id)
    {
        return $this->form()->update($id);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return mixed
     */
    public function store()
    {
        return $this->form()->store();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        return $this->form()->destroy($id);
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid($this->model);
        $table = $this->model->getTable();
        $fields = get_grid($table);
        foreach ($fields as $key => $value) {
            if ($key == 'id') {
                $grid->column('id', 'ID')->sortable();
                continue;
            }
            switch ($value['type']) {
                case 'image':
                    $grid->column($value['title'])->display(function () use ($key) {
                        return "<img src='" . get_image($this->$key) . "' style='width:60px;height:60px' />";
                    });
                    break;
                case 'file':
                    $grid->column($value['title'])->display(function ($key) {
                        return "<a href='" . $this->$key . "'>文件</a>";
                    });
                    break;
                default:
                    $grid->column($key, $value['title']);
                    break;
            }
        }


        $grid->actions(function (Grid\Displayers\Actions $actions) {
            if ($actions->getKey() == 1) {
                $actions->disableDelete();
            }
        });

        $grid->tools(function (Grid\Tools $tools) {
            $tools->batch(function (Grid\Tools\BatchActions $actions) {
                $actions->disableDelete();
            });
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show($this->model->findOrFail($id));

        $table = $this->model->getTable();
        $fields = get_show($table);

        foreach ($fields as $key => $value) {
            switch ($value['type']) {
                case 'image':
                    $show->$key()->image();
                    break;
                case 'file':
                    $show->$key()->file();
                    break;
                default:
                    $show->field($key, $value['title']);
                    break;
            }
        }
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {
        $form = new Form($this->model);
        $table = $this->model->getTable();
        if ($form->isCreating()) {
            $fields = get_create_form($table);
        } else {
            $fields = get_update_form($table);
        }
        //dd($fields);
        foreach ($fields as $field => $value) {
            $i = 0;
            $has = 0;
            foreach ($value as $k => $v) {
                if ($i === 0) {
                    if (is_numeric($field)) {
                        $form->$k($v);
                        continue;
                    }
                    $v = str2arr($v);
                    if ($className = Form::findFieldClass($k)) {
                        $element = new $className($field, $v);
                        $has = 1;
                    } else {
                        continue;
                    }
                } else {
                    if (is_null($v)) {
                        $element = $element->$k();
                    } else {
                        $element = $element->$k($v);
                    }
                }
                $i++;
            }
            if ($has)  $form->pushField($element);
        }

        return $form;
    }
}
